#!/usr/bin/env bash
# encoding: utf-8.0

set -o errexit

OUT_FILE=out.json

function split_string()
{
  local splitFlag="$1"
  local params="$2"
  IFS=${splitFlag}
  to_array=()
  i=0
  for dir in $params
  do
    to_array[$i]=${dir}
    i+=1
  done
  
  i+=1
  to_array[$i]=$(uuidgen)

  echo ${to_array[*]}
}

function main()
{
  iconv -f gbk -t utf-8 stdnames.csv > stdnames-utf-8.csv

  rm -f ${OUT_FILE}
  touch ${OUT_FILE}
  echo -n "{\"dicts\":[" >> ${OUT_FILE}
  while read line
  do 
    pair=($(split_string ',' ${line}))
    #echo ${pair[0]} " -> " ${pair[1]}
    name=${pair[0]}
    code=${pair[1]}
    uuid=${pair[2]}
    echo ${name} "->" ${code} "->" ${uuid}
    #echo ${code} "->" ${name} "->" ${uuid}
    #echo \{\"defKey\":\"${value},
    #echo "{\"defKey\":\""${value}"\",\"defName\":\""${key}"\",\"sort\":\"\",\"intro\":\"\",\"id\":"${uuid}",\"item\":[]}" 
  done < stdnames-utf-8.csv
  echo -n "]}" >> ${OUT_FILE}
}

$@
